package com.tech.zhidengxian.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcloud.cos.utils.Jackson;
import com.tech.zhidengxian.mapper.PurchaseMapper;
import com.tech.zhidengxian.model.dataobject.GoodsDO;
import com.tech.zhidengxian.model.dataobject.PurchaseDO;
import com.tech.zhidengxian.model.dto.GoodsDTO;
import com.tech.zhidengxian.model.dto.PurchaseDTO;
import com.tech.zhidengxian.model.dto.page.MyPage;
import com.tech.zhidengxian.model.dto.request.PurchaseRequest;
import com.tech.zhidengxian.service.api.PurchaseService;
import com.tech.zhidengxian.util.JacksonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Ljh
 * @date 2023/5/11 1:34
 * @description
 **/
@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Resource
    private PurchaseMapper purchaseMapper;


    @Override
    public MyPage<PurchaseDTO> pageQuery(PurchaseRequest purchaseRequest) {
        IPage<PurchaseDO> iPage = purchaseMapper.pageQuery(purchaseRequest);
        List<PurchaseDO> records = iPage.getRecords();
        List<PurchaseDTO> purchaseDTOS = JacksonUtils.convertList(records, PurchaseDTO.class);
        return MyPage.of(purchaseDTOS,purchaseRequest.getCurrentPage(),purchaseRequest.getPageSize(),iPage.getTotal());
    }

    @Override
    public PurchaseDTO publish(PurchaseDTO purchaseDTO) {
        PurchaseDO purchaseDO = JacksonUtils.convert(purchaseDTO, PurchaseDO.class);
        purchaseMapper.insert(purchaseDO);
        return JacksonUtils.convert(purchaseDO,PurchaseDTO.class);
    }
}
